package com.sale.succ.components.orders;

import static com.sale.succ.utils.TraderHelper.getLabel;

import java.util.ArrayList;
import java.util.HashMap;

import com.sale.succ.components.DefInput;
import com.sale.succ.components.MessDialog;

import com.sale.succ.objects.OrderObj;

import com.sale.succ.objects.ProductObj;
import com.sale.succ.objects.Session;
import com.sale.succ.system.txt.ObjectsProvider;
import com.sale.succ.utils.Logger;
import com.sale.succ.utils.SystemConstants;
import com.sale.succ.utils.TraderConstants;
import com.sale.succ.utils.TraderHelper;

import eve.fx.Color;
import eve.fx.Point;
import eve.ui.Control;
import eve.ui.Gui;

import eve.ui.table.TableCellAttributes;
import eve.ui.table.TableModel;

public class OrdersTableModel extends TableModel {
	private HashMap currentRests = new HashMap();

	private MessDialog loadingDialog;
	private String HEADERS[] = { getLabel(TraderConstants.ORDER_PRODUCT_COL),
			getLabel(TraderConstants.ORDER_PRICE_COL),
			getLabel(TraderConstants.ORDER_ORDER_COL) };
	private ArrayList data;

	private final int NAME_COL_WIDTH = (int) (Gui.getScreenSize().width * 0.6);
	private static final int NAME = 0;
	private static final int ORDER = 2;

	public OrdersTableModel() {
		loadingDialog = new MessDialog(getLabel(TraderConstants.LOADING), MessDialog.NOB);
		loadingDialog.exec();
		try {
			initData();
		} finally {
			loadingDialog.close(0);
		}

		numRows = data.size();
		numCols = HEADERS.length;
		hasRowHeaders = false;
		hasColumnHeaders = true;
		fillToEqualWidths = true;

	}

	public HashMap getRests() {
		return currentRests;
	}

	private void initData() {
		data = ObjectsProvider.getInstance().getOrderObjCollection(Session.getInstance().getClientId());
	}

	public int calculateRowHeight(int row) {
		return 20;
	}

	public OrderObj getDataObject(int row) {
		return (OrderObj) data.get(row);
	}

	public double getSum() {
		double sum = 0;
		for (int i = 0; i < data.size(); i++) {
			OrderObj obj = (OrderObj) data.get(i);
			int order;
			if ((order = Integer.parseInt(obj.getOrder())) != 0) {
				sum += order * Double.parseDouble((obj.getPrice()));
			}
		}
		return sum;
	}

	public int calculateColWidth(int col) {
		if (col == -1)
			return 0;
		if (col != NAME) {
			return (Gui.getScreenSize().width - NAME_COL_WIDTH - SystemConstants.SCROLL_BUTTON_W)
					/ (HEADERS.length - 1);
		}
		return NAME_COL_WIDTH;
	}

	public Object getCellData(int row, int col) {
		if (row == -1) {
			return HEADERS[col];
		}
		OrderObj obj = (OrderObj) data.get(row);
		switch (col) {
		case 0:
			ProductObj product = (ProductObj) ObjectsProvider.getInstance()
					.getProductCollection().get(obj.getProductId());
			return product.getProductName();
		case 1:
			return obj.getPrice();
		case 2:
			return obj.getOrder();

		default:
			return "unknown";
		}

	}

	private DefInput editor;

	private DefInput getEditor() {
		if (editor == null) {
			editor = new DefInput();
		}
		return editor;
	}

	protected Control getControlFor(int row, int col) {
		if (col == ORDER && row != -1) {
			OrderObj obj = (OrderObj) data.get(row);
			if (obj == null) {
				getEditor().setText("0");
			} else {
				getEditor().setText(obj.getOrder());
			}

			return getEditor();
		}
		return null;
	}

	protected void notifyDataChange(Point cell) {
		try {
			if (cell.y == -1)
				return;
			OrderObj obj = (OrderObj) data.get(cell.y);

			int previousOrder = Integer.parseInt(obj.getOrder());
			int currentOrder = Integer.parseInt(getEditor().getText());

			obj.setOrder(currentOrder + "");
			int prevInMap = 0;
			if(currentRests.get(obj.getProductId())!=null){
				prevInMap = Integer.parseInt(currentRests.get(obj.getProductId()).toString());
				
			}
			currentRests.put(obj.getProductId(), prevInMap + (currentOrder - previousOrder)
					+ "");

			ArrayList list = new ArrayList();
			list.add(getSum() + "");
			((OrdersTable) table).notifyListener(list, true);
		} catch (Exception e) {
			Logger.error(e.getMessage());
			new MessDialog(TraderHelper
					.getLabel(TraderConstants.MSG_WRONG_ORDER)).showAsPopup();
		}
	}

	public ArrayList getOrders() {
		return data;
	}

	public TableCellAttributes getCellAttributes(int row, int col,
			boolean isSelected, TableCellAttributes ta) {
		ta = super.getCellAttributes(row, col, isSelected, ta);
		if (row != -1) {
			ta.clipCellData = true;
			ta.anchor = WEST;
		}
		if (isSelected) {
			if (row != -1) {
				ta.foreground = Color.Black;
				ta.fillColor = TraderConstants.TBLMODEL_SELCOL;
			}
		} else {
			ta.foreground = Color.DarkGray;
		}
		if (row != -1) {
			OrderObj obj = (OrderObj) data.get(row);
			if (obj.getOrder().equals("0")) {
				ta.foreground = Color.Red;
			} else {
				ta.foreground = Color.DarkGray;
			}
		}
		return ta;
	}
}
